import requests
from bs4 import BeautifulSoup
import warnings
warnings.filterwarnings("ignore")

def crawl_university_ranking():
    url = "http://www.shanghairanking.cn/rankings/bcur/2020"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
    }
    try:
        response = requests.get(url, headers=headers, verify=False)
        response.raise_for_status()
        response.encoding = response.apparent_encoding

        soup = BeautifulSoup(response.text, "lxml")

        table = soup.find("table", class_="rk-table")

        rows = table.find_all("tr")[1:]
        print("=" * 80)
        print(f"{'排名':<4}{'学校名称':<20}{'英文名称':<20}{'标签':<10}{'省市':<6}{'学校类型':<8}{'总分':<6}")
        print("-" * 80)
        for row in rows:
            cols = row.find_all("td")
            if len(cols) < 5:
                continue
            rank = cols[0].text.strip()
            name_info = cols[1].find_all("div")
            name = name_info[0].text.strip() if len(name_info) > 0 else ""
            eng_name = name_info[1].text.strip() if len(name_info) > 1 else ""
            tag = name_info[2].text.strip() if len(name_info) > 2 else ""
            province = cols[2].text.strip()
            type_ = cols[3].text.strip()
            score = cols[4].text.strip()
            print(f"{rank:<4}{name:<20}{eng_name:<20}{tag:<10}{province:<6}{type_:<8}{score:<6}")
        print("=" * 80)
    except Exception as e:
        print(f"爬取失败，错误原因：{str(e)}")
if __name__ == "__main__":
    crawl_university_ranking()